<template>
  <div>
    <el-container style="height: 800px; border: 1px solid #eee">
      <el-aside  width="200px" style="background-color: rgb(238, 241, 246)">
        <Aside></Aside>
      </el-aside>
      <el-container>
        <el-header style="text-align: right; font-size: 12px;">
          <Head></Head>
        </el-header>
        <el-main>
          <el-table
              :data="tableData"
              style="width: 100%">
            <el-table-column
                prop="teacherId"
                label="教职工号"
                >
            </el-table-column>
            <el-table-column
                prop="teacherName"
                label="教师姓名"
                >
            </el-table-column>
            <el-table-column
                prop="courseName"
                label="教授课程"
                >
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="100px">
              <template #default="scope">
                <el-button @click="editInfo(scope.row)" type="text" size="small">编辑</el-button>
                <el-button type="text" size="small" @click="confirmDelete(scope.row.teacherId)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-dialog title="编辑" v-model="dialogFormVisible">
            <el-form :model="form">
              <!--              <el-form-item label="学生学号" :label-width="formLabelWidth">-->
              <!--                <el-input v-model="form.studentId" autocomplete="off"></el-input>-->
              <!--              </el-form-item>-->
              <el-form-item label="教师姓名" :label-width="formLabelWidth">
                <el-input v-model="form.teacherName" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="教授课程" :label-width="formLabelWidth">
                <el-input v-model="form.courseName" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
                    <span class="dialog-footer">
                      <el-button @click="dialogFormVisible = false">取 消</el-button>
                      <el-button type="primary" @click="pushEditInfo(doMethod)">确 定</el-button>
                    </span>
            </template>
          </el-dialog>

          <el-button class="el-button" size="medium" @click="addInfo">新增</el-button>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import Head from "@/components/Head";
import Aside from "@/components/Aside";
export default {
  components: {
    Head,
    Aside
  },
  name: "Teacher",
  data() {
    return {
      tableData : [],
      formLabelWidth : '120px',
      dialogFormVisible: false,
      doMethod : '',
      form: {
        teacherId : '',
        teacherName: '',
        courseName: ''
      }
    }
  },
  created() {
    this.getTeacherList()
  },
  methods: {
    getTeacherList() {
      const _this = this
      _this.axios.get("/teacher/list").then(res => {
        console.log(res.data.data)
        this.tableData = res.data.data
      })
    },
    handleClick(row) {
      console.log(row)
    },
  editInfo(row) {
    this.form.teacherId = row.teacherId
    this.form.teacherName = row.teacherName
    this.form.courseName = row.courseName
    this.dialogFormVisible = true
    this.doMethod = 'edit'
  },
  pushEditInfo(action) {
    const _this = this;
    this.axios.post("/teacher/" + action, this.form).then(res => {
      console.log(res)
      _this.dialogFormVisible = false
      _this.getTeacherList()
    })
  },
  addInfo() {
    this.form.teacherId = ''
    this.form.teacherName = ''
    this.form.courseName = ''
    this.dialogFormVisible = true
    this.doMethod = 'add'
  },
  confirmDelete(id) {
    this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      const _this = this;
      this.axios.post("/teacher/del/" + id).then(res => {
        console.log(res)
        _this.getTeacherList()
      })
      // this.$message({
      //   type: 'success',
      //   message: '删除成功!'
      // });
    }).catch(() => {
      this.$message({
        type: 'info',
        message: '已取消删除'
      });
    });
  }
  }
}
</script>

<style scoped>

</style>
